//
//  QKPickerView.swift
//  HHSwift
//
//  Created by 华宏 on 2019/10/2.
//  Copyright © 2019 huahong. All rights reserved.
//

import UIKit

protocol QKPickerViewDelegate {
    func pickerView(_ pickerView: QKPickerView, didSelectRow row: Int, selectText text: String)
}

class QKPickerView: UIView {

    typealias SureBtnBlock = (_ selectedRow:Int,_ selectedText:String) -> Void
    var sureBtnBlock:SureBtnBlock?
    var delegate:QKPickerViewDelegate?
    
    var dataArray:Array<String>!
  fileprivate  var selectedText:String!
  fileprivate  var selectedRow : Int = 0
        
        
//     //单例模式
//        static var shared:QKPickerView? = QKPickerView(){
//            didSet{
//                if shared == nil{
//                    shared = QKPickerView()
//
//                }
//            }
//
//        }

    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        self.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0, alpha: 0.3)
        
        self.addSubview(self.pickerView)
        self.addSubview(self.toolBar)
    }
    
    init(dataArray:Array<String>) {
      
        self.init()
        
        self.dataArray = dataArray

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        
    
    func show() {
           
       if dataArray.count == 0 {
           return
       }
       
       self.selectedText = dataArray.first

       UIApplication.shared.keyWindow?.addSubview(self)
    
     }
    
//MARK: - lazy
    
    lazy var toolBar: UIToolbar = {
        let toolBar = UIToolbar.init(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height - self.pickerView.bounds.size.height - 40, width: UIScreen.main.bounds.size.width, height: 40))
        toolBar.backgroundColor = .white
        let leftItem = UIBarButtonItem.init(title: "  取消", style: .plain, target: self, action: #selector(cancelBtnClicked))
        let centerItem = UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let rightItem = UIBarButtonItem(title: "确定  ", style: .plain, target: self, action: #selector(sureBtnClicked))
        toolBar.items = [leftItem,centerItem,rightItem]
        return toolBar
    }()
    
    lazy var pickerView: UIPickerView = {
        let pickerView = UIPickerView.init(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height - 216, width: UIScreen.main.bounds.size.width, height: 216))
            pickerView.delegate = self
            pickerView.dataSource = self
            pickerView.backgroundColor = .white
        
        return pickerView
    }()
    
//MARK: - Action:
    @objc func cancelBtnClicked() {
        UIApplication.shared.keyWindow?.subviews.last?.removeFromSuperview()
    }
    
    @objc func sureBtnClicked() {

        sureBtnBlock?(self.selectedRow,self.selectedText)
        
        delegate?.pickerView(self, didSelectRow: self.selectedRow, selectText: self.selectedText)
        
        
        UIApplication.shared.keyWindow?.subviews.last?.removeFromSuperview()
       
    }
        
        
        
    }

    extension QKPickerView:UIPickerViewDelegate,UIPickerViewDataSource{
        
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            
            return dataArray.count
        }
        
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

            return dataArray[row]
        }
        
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
           
            self.selectedRow = row
            self.selectedText = self.dataArray[row]
        }
        
    }
